package com.ymatou.iapi.productspu.testcase;

import com.ymatou.iapi.productspu.parameter.QuerySPUListBean;
import com.ymatou.iapi.productspu.parameter.SPUDTO;
import com.ymatou.iapi.productspu.service.QuerySPUListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymttest.database.sqlwapper.SpuPropertyWapper;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.math.BigDecimal;
import java.util.*;

/**
 * 获取SPU列表 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_QuerySPUList {
	private static QuerySPUListBean queryspulistBean;
	private static QuerySPUListCall queryspulistCall;
	SpuPropertyWapper SpuPropertyWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取SPU列表");
	}

	@Before
	public void caseUp() {
		queryspulistBean = new QuerySPUListBean();
		queryspulistCall = new QuerySPUListCall();
		SpuPropertyWapper = new SpuPropertyWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(queryspulistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verifyDB(SPUDTO spu) throws InterruptedException {
		Integer spuId = spu.getSPUId();
		Logger.comment("验证spuid:" + spuId);
		
		Map spuinfo = SpuPropertyWapper.selectSpuProductInfoBySpuid(spuId);
		Logger.debug(spuinfo);
		Logger.verifyEquals(spuinfo.get("BarCode") == null ? null : spuinfo.get("BarCode"), spu.getBarCode(), "验证BarCode");
		Logger.verifyEquals(spuinfo.get("BrandEnName") == null ? null : spuinfo.get("BrandEnName"), spu.getBrandEnName(), "验证BrandEnName");
		Logger.verifyEquals(spuinfo.get("BrandId") == null ? 0 : spuinfo.get("BrandId"), spu.getBrandId(), "验证BrandId");
		Logger.verifyEquals(spuinfo.get("BrandName") == null ? null : spuinfo.get("BrandName"), spu.getBrandName(), "验证BrandName");
		Logger.verifyEquals(spuinfo.get("Height") == null ? 0 : spuinfo.get("Height"), spu.getHeight(), "验证Height");
		Logger.verifyEquals(spuinfo.get("IsPspSpu") == null ? null : spuinfo.get("IsPspSpu"), spu.getIsPspSpu(), "验证IsPspSpu");
		Logger.verifyEquals(spuinfo.get("IsRecmd") == null ? false : spuinfo.get("IsRecmd"), spu.getIsRecmd(), "验证IsRecmd");
		Logger.verifyEquals(spuinfo.get("Length") == null ? 0 : spuinfo.get("Length"), spu.getLength(), "验证Length");
		 Logger.verifyEquals(spuinfo.get("ListingTime")==null?null:spuinfo.get("ListingTime"),spu.getMarketingTime(),"验证MarketingTime");
		Logger.verifyEquals(spuinfo.get("MasterCategoryId") == null ? null : spuinfo.get("MasterCategoryId"), spu.getMasterCategoryId(), "验证MasterCategoryId");
		Logger.verifyEquals(spuinfo.get("MasterCategoryName") == null ? null : spuinfo.get("MasterCategoryName"), spu.getMasterCategoryName(), "验证MasterCategoryName");
		Logger.verifyEquals(spuinfo.get("CountryName") == null ? null : spuinfo.get("CountryName"), spu.getOrigin(), "验证Origin");
		Logger.verifyEquals(spuinfo.get("PriceEnd") == null ? null : new BigDecimal(spuinfo.get("PriceEnd").toString()).setScale(2), spu.getPriceEnd().setScale(2), "验证PriceEnd");
		Logger.verifyEquals(spuinfo.get("PriceStart") == null ? null : new BigDecimal(spuinfo.get("PriceStart").toString()).setScale(2), spu.getPriceStart().setScale(2),
				"验证PriceStart");
		Logger.verifyEquals(spuinfo.get("DescText") == null ? null : spuinfo.get("DescText"), spu.getProductDescription(), "验证ProductDescription");
		Logger.verifyEquals(spuinfo.get("ProductName") == null ? null : spuinfo.get("ProductName"), spu.getProductName(), "验证ProductName");
		// Logger.verifyEquals(spuinfo.get("ProductPictures")==null?null:spuinfo.get("ProductPictures"),spu.getProductPictures(),"验证ProductPictures");
		Logger.verifyEquals(spuinfo.get("Summary") == null ? null : spuinfo.get("Summary"), spu.getProductSummary(), "验证ProductSummary");
		Logger.verifyEquals(spuinfo.get("SecondCategoryId") == null ? null : spuinfo.get("SecondCategoryId"), spu.getSecondCategoryId(), "验证SecondCategoryId");
		Logger.verifyEquals(spuinfo.get("SecondCategoryName") == null ? null : spuinfo.get("SecondCategoryName"), spu.getSecondCategoryName(), "验证SecondCategoryName");
		Logger.verifyEquals(spuinfo.get("SpuId") == null ? null : spuinfo.get("SpuId"), spu.getSPUId(), "验证SPUId");
		Logger.verifyEquals(spuinfo.get("ThirdCategoryId") == null ? null : spuinfo.get("ThirdCategoryId"), spu.getThirdCategoryId(), "验证ThirdCategoryId");
		Logger.verifyEquals(spuinfo.get("ThirdCategoryName") == null ? null : spuinfo.get("ThirdCategoryName"), spu.getThirdCategoryName(), "验证ThirdCategoryName");
		Logger.verifyEquals(spuinfo.get("Unit") == null ? null : spuinfo.get("Unit"), spu.getUnit(), "验证Unit");
		Logger.verifyEquals(spuinfo.get("Weight") == null ? 0 : spuinfo.get("Weight"), spu.getWeight(), "验证Weight");
		Logger.verifyEquals(spuinfo.get("Width") == null ? 0 : spuinfo.get("Width"), spu.getWidth(), "验证Width");
		Logger.verifyEquals(spuinfo.get("Status")==null?0:spuinfo.get("Status"),spu.getStatus(),"验证Status");

		List<String> pp = spu.getProductPictures();
		Collections.sort(pp);
		List<String> pp2 = (List<String>) (spuinfo.get("ProductPics") == null ? new ArrayList<>() : Arrays.asList(spuinfo.get("ProductPics").toString().split(",")));
		Collections.sort(pp2);
		Logger.verifyEquals(pp2.toString(), pp.toString(), "验证ProductPictures");

		List<String> pdp = spu.getProductDescriptionPics();
		Collections.sort(pp);

		List<String> pdp2 = (List<String>) (spuinfo.get("DescPics") == null ? new ArrayList<>() : Arrays.asList(spuinfo.get("DescPics").toString().split(",")));
		Collections.sort(pp2);
		Logger.verifyEquals(pdp2.toString(), pdp.toString(), "验证ProductDescriptionPics");
//	spulist不展示属性详情 只在详情页展示
//		List<Map> pinfo = SpuPropertyWapper.selectSpuPropertyInfosBySpuid(spuId);
//		Logger.debug("pinfo:"+pinfo);
//		Map<String, Map> mpinfo = new HashMap<>();
//		pinfo.forEach(m -> mpinfo.put(m.get("Values").toString(), m));
//		Logger.verifyEquals(pinfo.size(), spu.getPropertyPairs().size(), "验证PropertyPairs的size");
//		spu.getPropertyPairs().forEach(pps -> {
//			Map mpp = mpinfo.get(pps.getPropertyValue());
//			Logger.verifyEquals(mpp.get("PropertyName") == null ? null : mpp.get("PropertyName").toString(), pps.getPropertyName(), "验证PropertyName");
//			Logger.verifyEquals(mpp.get("PropertyId") == null ? null : mpp.get("PropertyId"), pps.getPropertyId(), "验证PropertyId");
//			Logger.verifyEquals(mpp.get("Status") == null ? null : mpp.get("Status").toString(), pps.getPropertyStatus().toString(), "验证PropertyStatus");
//		});
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QuerySPUList_001() {
		Logger.start(true, "获取SPU列表成功");
		try {
			queryspulistBean.setPageIndex(1);
			queryspulistBean.setPageSize(10);
			queryspulistCall.setData(queryspulistBean);
			queryspulistCall.callService();
			Logger.verifyEquals("200", queryspulistCall.getString("Status"), "验证Status");
//			
			queryspulistCall.getSPUs().forEach(spu -> {
				try {
					verifyDB(spu);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	@Ignore
	//正常返回 默认第一页
	public void Tc_QuerySPUList_002() {
		Logger.start(true, "不输入页码失败");
		try {
			queryspulistBean.setPageSize(10);
			queryspulistCall.setData(queryspulistBean);
			queryspulistCall.callService();
			// ?默认第一页?
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	@Ignore
	//有返回 0个数据
	public void Tc_QuerySPUList_003() {
		Logger.start(true, "不输入每页大小失败");
		try {
			queryspulistBean.setPageIndex(1);
			// queryspulistBean.setPageSize(10);
			queryspulistCall.setData(queryspulistBean);
			queryspulistCall.callService();
			// 默认0个?
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_QuerySPUList_004() {
		Logger.start(true, "SpuKeyword查询成功");
		try {
			String spukey = "测试商品";
			queryspulistBean.setSpuKeyword(spukey);
			queryspulistBean.setPageIndex(1);
			queryspulistBean.setPageSize(10);
			queryspulistCall.setData(queryspulistBean);
			queryspulistCall.callService();
			Logger.verifyEquals("200", queryspulistCall.getString("Status"), "验证Status");
			
			queryspulistCall.getSPUs().forEach(spu -> {
				Logger.verifyIsContains(spukey, spu.getProductName(), "验证spukey");
			});
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_QuerySPUList_005() {
		Logger.start(true, "BrandName查询成功");
		try {
			String bname = "耐克";
			queryspulistBean.setBrandName(bname);
			queryspulistBean.setPageIndex(1);
			queryspulistBean.setPageSize(10);
			queryspulistCall.setData(queryspulistBean);
			queryspulistCall.callService();
			Logger.verifyEquals("200", queryspulistCall.getString("Status"), "验证Status");
			
			queryspulistCall.getSPUs().forEach(spu -> {
				Logger.verifyIsContains(bname, spu.getBrandName(), "验证BrandName");
			});

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	@Ignore
	public void Tc_QuerySPUList_006() {
		Logger.start(true, "ProductName查询成功");
		try {
			//于推荐接口查询 ProductName是推荐商品时候返回字段中的IsPspSpu IsRecmd 为true
			//？？ 需要数据
			String pn="耐克";
			queryspulistBean.setSpuKeyword(pn);
			queryspulistBean.setProductName(pn);
			queryspulistBean.setPageIndex(1);
			queryspulistBean.setPageSize(10);
			queryspulistCall.setData(queryspulistBean);
			queryspulistCall.callService();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_QuerySPUList_007() {
		Logger.start(true, "MasterCategoryId查询成功");
		try {
			Integer MasterCategoryId = 1001;
			queryspulistBean.setMasterCategoryId(MasterCategoryId);
			queryspulistBean.setPageIndex(1);
			queryspulistBean.setPageSize(10);
			queryspulistCall.setData(queryspulistBean);
			queryspulistCall.callService();
			Logger.verifyEquals("200", queryspulistCall.getString("Status"), "验证Status");
			
			queryspulistCall.getSPUs().forEach(spu -> {
				Logger.verifyIsContains(MasterCategoryId, spu.getMasterCategoryId(), "验证MasterCategoryId");
			});

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_QuerySPUList_008() {
		Logger.start(true, "SecondCategoryId查询成功");
		try {
			Integer SecondCategoryId = 1002;
			queryspulistBean.setSecondCategoryId(SecondCategoryId);
			queryspulistBean.setPageIndex(1);
			queryspulistBean.setPageSize(10);
			queryspulistCall.setData(queryspulistBean);
			queryspulistCall.callService();
			Logger.verifyEquals("200", queryspulistCall.getString("Status"), "验证Status");
			
			queryspulistCall.getSPUs().forEach(spu -> {
				Logger.verifyIsContains(SecondCategoryId, spu.getSecondCategoryId(), "验证SecondCategoryId");
			});

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_QuerySPUList_009() {
		Logger.start(true, "SecondCategoryId查询成功");
		try {
			Integer SecondCategoryId = 1003;
			queryspulistBean.setSecondCategoryId(SecondCategoryId);
			queryspulistBean.setPageIndex(1);
			queryspulistBean.setPageSize(10);
			queryspulistCall.setData(queryspulistBean);
			queryspulistCall.callService();
			Logger.verifyEquals("200", queryspulistCall.getString("Status"), "验证Status");
			
			queryspulistCall.getSPUs().forEach(spu -> {
				Logger.verifyIsContains(SecondCategoryId, spu.getSecondCategoryId(), "验证ThirdCategoryId");
			});


		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_QuerySPUList_010() {
		Logger.start(true, "翻页查询成功");
		try {
			String spukey = "测试商品";
			queryspulistBean.setSpuKeyword(spukey);
			queryspulistBean.setPageIndex(1);
			queryspulistBean.setPageSize(10);
			queryspulistCall.setData(queryspulistBean);
			queryspulistCall.callService();
			Logger.verifyEquals("200", queryspulistCall.getString("Status"), "验证Status");
			int TotalCount=queryspulistCall.getJsonObject().get("Result").getAsJsonObject().get("TotalCount").getAsInt();
			Logger.verifyEquals(TotalCount>10?10:TotalCount, queryspulistCall.getSPUs().size(), "验证条数");
			queryspulistCall.getSPUs().forEach(spu -> {
				Logger.verifyIsContains(spukey, spu.getProductName(), "验证spukey");
			});
			TotalCount=queryspulistCall.getJsonObject().get("Result").getAsJsonObject().get("TotalCount").getAsInt();
			int lastpage=TotalCount/10+1;
			queryspulistBean.setPageIndex(lastpage);
			queryspulistCall.callService();
			Logger.verifyEquals("200", queryspulistCall.getString("Status"), "验证Status");
			Logger.verifyEquals(TotalCount%10, queryspulistCall.getSPUs().size(), "验证条数");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_QuerySPUList_011() {
		Logger.start(true, "多条件查询成功");
		try {
			String spukey = "自动化测试";
			queryspulistBean.setSpuKeyword(spukey);
			String bname = "耐克";
			queryspulistBean.setBrandName(bname);
			Integer MasterCategoryId = 1001;
			queryspulistBean.setMasterCategoryId(MasterCategoryId);
			Integer SecondCategoryId = 1002;
			queryspulistBean.setSecondCategoryId(SecondCategoryId);
			Integer ThirdCategoryId = 1003;
			queryspulistBean.setSecondCategoryId(ThirdCategoryId);
			queryspulistBean.setPageIndex(1);
			queryspulistBean.setPageSize(10);
			queryspulistCall.setData(queryspulistBean);
			queryspulistCall.callService();
			Logger.verifyEquals("200", queryspulistCall.getString("Status"), "验证Status");
			
			queryspulistCall.getSPUs().forEach(spu -> {
				Logger.verifyIsContains(spukey, spu.getProductName(), "验证spukey");
				Logger.verifyIsContains(bname, spu.getBrandName(), "验证BrandName");
				Logger.verifyIsContains(MasterCategoryId, spu.getMasterCategoryId(), "验证MasterCategoryId");
				Logger.verifyIsContains(SecondCategoryId, spu.getSecondCategoryId(), "验证SecondCategoryId");
				Logger.verifyIsContains(ThirdCategoryId, spu.getThirdCategoryId(), "验证ThirdCategoryId");
			});

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}